package com.xinke.edu.mypetadoption.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.xinke.edu.mypetadoption.Activity.adapter.OrderAdapter;
import com.xinke.edu.mypetadoption.R;
import com.xinke.edu.mypetadoption.databinding.ActivityMyOrderBinding;
import com.xinke.edu.mypetadoption.entity.Orders;
import com.xinke.edu.mypetadoption.entity.ResultBody;
import com.xinke.edu.mypetadoption.util.RetrofitUtil;
import com.xinke.edu.mypetadoption.util.SPUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class MyOrder extends AppCompatActivity {
    private ActivityMyOrderBinding binding;
    private OrderAdapter orderAdapter;
    
    private int orderTypeId = 0;
    private int pageIndex = 1;
    private int pageSize = 10;
    private List<Orders> orderList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMyOrderBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        
        // 初始化订单列表
        initOrderList();
        // 加载订单数据
        loadOrderData();
        // 返回按钮点击事件
        binding.exit.setOnClickListener(v -> finish());
        // 设置分类标签点击事件
        setCategoryTabListeners();
    }
    
    private void initOrderList() {
        orderAdapter = new OrderAdapter();
        binding.MyOrderRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.MyOrderRecyclerView.setAdapter(orderAdapter);
    }
    
    private void setCategoryTabListeners() {
        // 全部订单
        binding.tabAll.setOnClickListener(v -> {
            orderTypeId = 0;
            updateCategoryTabStatus(v);
            loadOrderData();
        });
        // 待付款
        binding.tabPendingPayment.setOnClickListener(v -> {
            orderTypeId = 0;
            updateCategoryTabStatus(v);
            loadOrderData();
        });
        // 待发货
        binding.tabPendingDelivery.setOnClickListener(v -> {
            orderTypeId = 1;
            updateCategoryTabStatus(v);
            loadOrderData();
        });
        // 待收货
        binding.tabPendingReceipt.setOnClickListener(v -> {
            orderTypeId = 2;
            updateCategoryTabStatus(v);
            loadOrderData();
        });
        // 已完成
        binding.tabCompleted.setOnClickListener(v -> {
            orderTypeId = 3;
            updateCategoryTabStatus(v);
            loadOrderData();
        });
    }
    
    private void updateCategoryTabStatus(View selectedTab) {
        // 重置所有标签样式
        resetTabStyles();
        // 设置选中标签样式
        if (selectedTab instanceof TextView) {
            TextView tab = (TextView) selectedTab;
            tab.setTextColor(getResources().getColor(R.color.black));
            tab.setBackgroundResource(R.drawable.tab_underline);
        }
    }
    
    private void resetTabStyles() {
        binding.tabAll.setTextColor(getResources().getColor(R.color.black));
        binding.tabPendingPayment.setTextColor(getResources().getColor(R.color.black));
        binding.tabPendingDelivery.setTextColor(getResources().getColor(R.color.black));
        binding.tabPendingReceipt.setTextColor(getResources().getColor(R.color.black));
        binding.tabCompleted.setTextColor(getResources().getColor(R.color.black));
        
        binding.tabAll.setBackground(null);
        binding.tabPendingPayment.setBackground(null);
        binding.tabPendingDelivery.setBackground(null);
        binding.tabPendingReceipt.setBackground(null);
        binding.tabCompleted.setBackground(null);
    }
    
    private void loadOrderData() {
        // 直接获取int类型的userId
        int userId = SPUtils.getInstance(this).getInt("userId", 0);
        if (userId <= 0) {
            Toast.makeText(this, "用户ID无效", Toast.LENGTH_SHORT).show();
            return;
        }
        
        // 使用正确的API方法selectOrdersUser
        RetrofitUtil.getApi().selectOrdersUser(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultBody<List<Orders>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {}
                    
                    @Override
                    public void onNext(ResultBody<List<Orders>> resultBody) {
                        if (resultBody.getCode() == 200) {
                            List<Orders> data = resultBody.getData();
                            if (data != null && !data.isEmpty()) {
                                orderList.clear();
                                orderList.addAll(data);
                                // 使用适配器的setOrderList方法设置数据
                                orderAdapter.setOrderList(orderList);
                            } else {
                                orderList.clear();
                                orderAdapter.setOrderList(orderList);
                                Toast.makeText(MyOrder.this, "暂无订单", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(MyOrder.this, resultBody.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                    
                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(MyOrder.this, "网络请求失败", Toast.LENGTH_SHORT).show();
                    }
                    
                    @Override
                    public void onComplete() {}
                });
    }
}